package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;

    @Autowired
    DishFlavorMapper dishFlavorMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;


    /**
     * 新增菜品
     * @param dishDTO
     */
    @Transactional  //事务注解
    public void saveDishAndFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);

        Long dishId = dish.getId();


        //获取口味的集合
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //口味的集合不为空才插入
        if(flavors != null && flavors.size() > 0){
            flavors.forEach(dishFlavor -> {

                dishFlavor.setDishId(dishId);
            });
            //插入n条数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }


    /**
     * 菜品的分页查询
     * @param dishPageQueryDTO
     * @return
     */
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        List<DishVO> pageList= dishMapper.pageQuery01(dishPageQueryDTO);
        Page p = (Page)pageList;
        long total = p.getTotal();
        List result = p.getResult();

        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setRecords(result);
        return pageResult;
    }


    @Transactional
    public void delete01(List<Long> ids) {
        //起售状态的菜品不能删除
        for (Long id : ids) {
            Dish dish = dishMapper.statusQuery(id);
            if (dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //被套餐关联的菜品不能删除
        List<Long> list = setmealDishMapper.setmealDishQuery(ids);
        if (list != null && list.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品  (根据id删除菜品)
        for (Long id : ids) {
            dishMapper.getByIdDeleteDish(id);
        }
        //删除菜品的口味
        for (Long id : ids) {
            dishFlavorMapper.deleteFlavor(id);
        }

    }

    /**
     * 根据id查询菜品信息和口味信息
     * @param id
     * @return
     */
    @Transactional
    public DishVO queryDish(Long id) {
        //查询菜品信息
        Dish dish = dishMapper.statusQuery(id);
        //查询口味信息
        List<DishFlavor> listFlavor = dishFlavorMapper.flavorQuery(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(listFlavor);
        return dishVO;
    }





    public void updateWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //修改菜品的基本信息
        dishMapper.update(dish);
        //删除原先所有口味
        dishFlavorMapper.deleteFlavor(dish.getId());
        //添加传递过来的所有的口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors.size() > 0){
            //为口味设置每一个菜品的id
            flavors.forEach(flavor -> {
                flavor.setDishId(dishDTO.getId());
            });
            //插入n条数据
            dishFlavorMapper.insertBatch(flavors);
        }

    }





    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }

    /**
     * 菜品的起售与停售
     * @param status
     * @param dishId
     */
    @Override
    public void dishStatus01(Integer status, Long dishId) {
        dishMapper.updateDishStatus(status,dishId);
    }


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.flavorQuery(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
